﻿using System.IO;
using System.Xml;

namespace Koala.Pro.Common
{
    public sealed class AppConfig
    {
        private string filePath;

        public string HWSecurity => AppConfigGet("HWSecurity");

        public string System_ID => AppConfigGet("System_ID");

        public string AppName => AppConfigGet("ApplicationName");

        public string Manufacturer => AppConfigGet("Manufacturer");

        public AppConfig()
        {
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Web.Config");
            string path2 = AppDomain.CurrentDomain.FriendlyName + ".config";
            path2 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path2);
            string path3 = AppDomain.CurrentDomain.FriendlyName + ".dll.config";
            path3 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path3);
            if (File.Exists(path))
            {
                filePath = path;
                return;
            }
            if (File.Exists(path2))
            {
                filePath = path2;
                return;
            }
            if (File.Exists(path3))
            {
                filePath = path3;
                return;
            }
            throw new ArgumentNullException("没有找到Web.Config文件或者应用程序配置文件, 请指定配置文件");
        }

        public AppConfig(string configFilePath)
        {
            filePath = configFilePath;
        }

        public void AppConfigSet(string keyName, string keyValue)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(filePath);
            XmlNodeList elementsByTagName = xmlDocument.GetElementsByTagName("add");
            for (int i = 0; i < elementsByTagName.Count; i++)
            {
                XmlAttribute xmlAttribute = elementsByTagName[i].Attributes["key"];
                if (xmlAttribute != null && xmlAttribute.Value == keyName)
                {
                    xmlAttribute = elementsByTagName[i].Attributes["value"];
                    if (xmlAttribute != null)
                    {
                        xmlAttribute.Value = keyValue;
                        break;
                    }
                }
            }
            xmlDocument.Save(filePath);
        }

        public string AppConfigGet(string keyName)
        {
            string result = string.Empty;
            try
            {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(filePath);
                XmlNodeList elementsByTagName = xmlDocument.GetElementsByTagName("add");
                for (int i = 0; i < elementsByTagName.Count; i++)
                {
                    XmlAttribute xmlAttribute = elementsByTagName[i].Attributes["key"];
                    if (xmlAttribute != null && xmlAttribute.Value == keyName)
                    {
                        xmlAttribute = elementsByTagName[i].Attributes["value"];
                        if (xmlAttribute != null)
                        {
                            result = xmlAttribute.Value;
                            break;
                        }
                    }
                }
            }
            catch
            {
            }
            return result;
        }

        public string GetSubValue(string keyName, string subKeyName)
        {
            string[] array = AppConfigGet(keyName).ToLower().Split(new char[1] { ';' });
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i].ToLower().IndexOf(subKeyName.ToLower()) >= 0)
                {
                    int num = array[i].IndexOf("=");
                    return array[i].Substring(num + 1);
                }
            }
            return string.Empty;
        }

        public void SetConnectionString(string keyName, string keyValue)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(filePath);
            XmlNodeList elementsByTagName = xmlDocument.GetElementsByTagName("add");
            for (int i = 0; i < elementsByTagName.Count; i++)
            {
                XmlAttribute xmlAttribute = elementsByTagName[i].Attributes["name"];
                if (xmlAttribute != null && xmlAttribute.Value == keyName)
                {
                    xmlAttribute = elementsByTagName[i].Attributes["connectionString"];
                    if (xmlAttribute != null)
                    {
                        xmlAttribute.Value = keyValue;
                        break;
                    }
                }
            }
            xmlDocument.Save(filePath);
        }

        public string GetConnectionString(string keyName)
        {
            string result = string.Empty;
            try
            {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(filePath);
                XmlNodeList elementsByTagName = xmlDocument.GetElementsByTagName("add");
                for (int i = 0; i < elementsByTagName.Count; i++)
                {
                    XmlAttribute xmlAttribute = elementsByTagName[i].Attributes["name"];
                    if (xmlAttribute != null && xmlAttribute.Value == keyName)
                    {
                        xmlAttribute = elementsByTagName[i].Attributes["connectionString"];
                        if (xmlAttribute != null)
                        {
                            result = xmlAttribute.Value;
                            break;
                        }
                    }
                }
            }
            catch
            {
            }
            return result;
        }

        public DatabaseInfo GetDatabaseInfo(string keyName)
        {
            return new DatabaseInfo(GetConnectionString(keyName));
        }

        public void SetDatabaseInfo(string keyName, DatabaseInfo databaseInfo)
        {
            SetConnectionString(keyName, databaseInfo.ConnectionString);
        }
    }

}
